import { _decorator, Animation, Collider2D, Component, Contact2DType, Node } from 'cc';
const { ccclass, property } = _decorator;
import { Player, ShootType } from "./Player";

@ccclass('Reward')
export class Reward extends Component {

    @property
    speed = 80

    @property(Collider2D)
    collider: Collider2D = null

    @property(Animation)
    anim: Animation = null

    start() {
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onContactPlayer, this);
        }
    }

    update(deltaTime: number) {
        let p = this.node.position
        this.node.setPosition(p.x, p.y - this.speed * deltaTime, p.z)
        if (p.y < -550) {
            this.node.destroy()
        }
    }

    onContactPlayer(self, other: Collider2D) {
        console.log("吃到蘑菇啦");
        //获取节点名称
        const selfName = self.node.name
        const otherName = other.node.name
        //获取玩家的脚本组件
        const otherScript = other.node.getComponent('Player')
        console.log(otherScript);

        //判断该脚本是否是Player脚本
        if (otherScript instanceof Player) {
            //执行此奖励物品的奖励方法 
            this.RewardPlayer(otherScript)
            this.node.destroy()
        }

        console.log(`${selfName} 接触到物体 ${otherName}`);
    }

    RewardPlayer(player:Component){
            //@ts-ignore   //忽略函数未声明的错误
        player.changeShootType(ShootType.twoShoot)
    }
}



